﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PersonalSettingsOptionsPage.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2009
// </copyright>
// <summary>
//   Describes and manipulates the settings page.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace SettingsManager
{
    #region Namespace Imports

    using System;
    using System.Windows.Forms;

    using JetBrains.Annotations;
    using JetBrains.UI.Options;

    #endregion


    /// <summary>
    /// Describes and manipulates the settings page.
    /// </summary>
    [OptionsPage(_id, "Settings Manager", "SettingsManager.SettingsPageIcon.png", ParentId = "Tools")]
    public sealed partial class PersonalSettingsOptionsPage : UserControl, IOptionsPage
    {
        #region Constants and Fields

        /// <summary>
        /// Personal Settings Page ID.
        /// </summary>
        private const string _id = "SettingsManagerOptionsPage";

        private readonly ResharperSettingsManager _settingsManager;

        #endregion


        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PersonalSettingsOptionsPage"/> class.
        /// </summary>
        /// <param name="optionsUI">
        /// The options UI.
        /// </param>
        public PersonalSettingsOptionsPage([NotNull] IOptionsDialog optionsUI)
        {
            InitializeComponent();

            ResharperSettingsManager initialSettings = ResharperSettingsManager.Instance;

            _settingsManager = new ResharperSettingsManager();
            _settingsManager.Init();

            ConfigureSettingsManager(initialSettings, _settingsManager);

            _settingsFolderTextBox.Text = _settingsManager.SettingsFolderPath;
            _autoUpdateInterval.Value = _settingsManager.SettingsAutoUpdateInterval;
            _defaultSettingFileComboBox.SettingsManager = _settingsManager;
            _defaultSettingFileComboBox.SelectedFilePath = _settingsManager.DefaultSettingsFile;
        }

        #endregion


        #region Properties

        /// <summary>
        /// Gets the control to be shown as page.
        /// May be <c>Null</c> if the page does not have any UI.
        /// </summary>
        /// <value>
        /// Control to be shown as page.
        /// </value>
        public Control Control
        {
            get
            {
                return this;
            }
        }

        /// <summary>
        /// Gets the ID of this option page.
        /// <see cref="T:JetBrains.UI.Options.IOptionsDialog"/> or <see cref="T:JetBrains.UI.Options.OptionsPageDescriptor"/> could be used to retrieve the <see cref="T:JetBrains.UI.Options.OptionsManager"/> out of it.
        /// </summary>
        /// <value>
        /// ID of this option page.
        /// </value>
        public string Id
        {
            get
            {
                return _id;
            }
        }

        #endregion


        #region Public Methods

        /// <summary>
        /// Invoked when OK button in the options dialog is pressed.
        /// If the page returns <c>false</c>, the the options dialog won't be closed, and focus will be put into this page.
        /// </summary>
        /// <returns>
        /// The result of the operation.
        /// </returns>
        public bool OnOk()
        {
            // save values from controls
            _settingsManager.DefaultSettingsFile = _defaultSettingFileComboBox.SelectedFile != null
                                                       ? _defaultSettingFileComboBox.SelectedFile.Path.FullPath
                                                       : string.Empty;

            ResharperSettingsManager settings = ResharperSettingsManager.Instance;
            ConfigureSettingsManager(_settingsManager, settings);

            return true;
        }


        /// <summary>
        /// Check if the settings on the page are consistent, and page could be closed.
        /// </summary>
        /// <returns>
        /// <c>true</c> if page data is consistent.
        /// </returns>
        public bool ValidatePage()
        {
            return true;
        }

        #endregion


        #region Methods

        private static void ConfigureSettingsManager([NotNull] ResharperSettingsManager sourceSettingsManager, [NotNull] ResharperSettingsManager destinationSettingsManager)
        {
            destinationSettingsManager.SettingsFolderPath = sourceSettingsManager.SettingsFolderPath;
            destinationSettingsManager.SettingsAutoUpdateInterval = sourceSettingsManager.SettingsAutoUpdateInterval;
            destinationSettingsManager.UpdateSettingsAutomatically = sourceSettingsManager.UpdateSettingsAutomatically;
            destinationSettingsManager.DefaultSettingsFile = sourceSettingsManager.DefaultSettingsFile;
        }


        private void AutoUpdateIntervalValueChanged([NotNull] object sender, [NotNull] EventArgs e)
        {
            _settingsManager.SettingsAutoUpdateInterval = (int)_autoUpdateInterval.Value;
        }


        private void BrowseButtonClick([NotNull] object sender, [NotNull] EventArgs e)
        {
            DialogResult result = _settingsFolderBrowserDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                _settingsFolderTextBox.Text = _settingsFolderBrowserDialog.SelectedPath;
            }
        }


        private void SettingsFolderTextBoxTextChanged([NotNull] object sender, [NotNull] EventArgs e)
        {
            string settingsFolderPath = _settingsFolderTextBox.Text;
            _settingsManager.SettingsFolderPath = settingsFolderPath;
        }

        #endregion
    }
}